﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Web.UI.HtmlControls;
using PanasonicWarranty.Business;
using Utils;

namespace PanasonicWarranty.UI
{
    public partial class SmsMtStatistics : BasePageManageSms
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        void paging_PagingChange(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            pagingbottom.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            BindDataSmsStatistic();
            pagingtop.Visible = pagingbottom.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                pagingtop.PageDisplay = pagingbottom.PageDisplay = 6;
                BindDataDropDownList();
            }
        }

        private void BindDataDropDownList()
        {
            try
            {
                List<pa_Agency> list;
                bool isAdmin = IsRootAdmin();
                if (isAdmin)
                    list = pa_Agency.GetAll();
                else
                    list = pa_Agency.GetAll(CurUser.AgencyID.Value);
                pa_Agency.TransformLevel(ref list);
                foreach (var item in list)
                {
                    ddlAgency.Items.Add(new ListItem(item.AgencyName.ShowNameLevel(item.Level), item.AgencyID.ToString()));
                }
                if (CheckRole(Role.Recusive))
                    ddlAgency.Items.Insert(0, new ListItem(""));
            }
            catch { }
        }

        private void BindDataSmsStatistic()
        {
            try
            {
                string shortCode = null;
                string destAddr = null;
                DateTime? fromTime = null;
                DateTime? toTime = null;
                string status = null;
                int currentPage = pagingtop.CurrentPage;
                int pageSize = pagingtop.PageSize;
                int totalRow = 0;
                int totalMessage = 0;
                //int totalMessage = 0;
                if (!string.IsNullOrEmpty(ddlShortCode.SelectedValue)) shortCode = ddlShortCode.SelectedValue;
                if (!string.IsNullOrEmpty(txtPhoneNumber.Text.Trim())) destAddr = txtPhoneNumber.Text.Trim();
                if (!string.IsNullOrEmpty(txtFromDate.Text.Trim())) fromTime = Convert.ToDateTime(txtFromDate.Text.Trim());
                if (!string.IsNullOrEmpty(txtToDate.Text.Trim())) toTime = Convert.ToDateTime(txtToDate.Text.Trim());
                if (!string.IsNullOrEmpty(ddlStatus.SelectedValue)) status = ddlStatus.SelectedValue;
                int? agencyId = ddlAgency.SelectedValue == "" ? (int?)null : Convert.ToInt32(ddlAgency.SelectedValue);
                grvData.DataSource = SmsMtStatistic.Paging(currentPage, pageSize, ref totalRow, ref totalMessage, shortCode, destAddr, fromTime, toTime, status, agencyId);
                pagingtop.TotalRecord = pagingbottom.TotalRecord = totalRow;
                pagingtop.Visible = pagingbottom.Visible = pagingtop.TotalPage > 1;
                grvData.DataBind();
                ltrInfo.Text = "Tổng cộng bản tin: " + totalMessage;
            }
            catch(Exception ex) {
                HandleException(ex);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
            BindDataSmsStatistic();
        }

        protected void tSortable_length_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                BindDataSmsStatistic();
            }
            catch { }
        }
    }
}